package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.kxf.androidtestdemo.R;

public class ViewStubActivity extends AppCompatActivity {

    private ViewStub vs = null;
    private TextView view_stub_item_tv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_stub);
        vs = findViewById(R.id.vs);
        view_stub_item_tv = findViewById(R.id.view_stub_item_tv);//无效，还没解析布局
    }



    public void showVs(View view) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (vs.getParent()!=null){//利用ViewStub的parent来判断。当ViewStub被inflate后，getParent返回值是null
                    vs.inflate();
                    view_stub_item_tv = findViewById(R.id.view_stub_item_tv);
                }
            }
        });
    }

    public void setTextViewTxt(View view) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    view_stub_item_tv.setText("ViewStub " + System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
